TestCase
(
	'org.puremvc.js.core.ViewTest'
,	{
		
		/**
		 * @type {string}
		 */
		lastNotification: null
		
	,	onRegisterCalled: false
	
	,	onRemoveCalled: false
		
		/**
		 * @private
		 * @type {number}
		 */
	,	viewTestVar: null
	
	,	testGetInstance: function ()
		{
			var view= View.getInstance()
			
			assertNotNull('View#getInstance returned a non-null value', view);
			assertInstanceOf('View#getInstance returned the view instance', View, view);
		}
		
	,	testRegisterAndNotifyObserver: function ()
		{
			var view= View.getInstance()
			,	observer= new Observer(this.viewTestMethod, this)
			,	note= ViewTestNote.create(10);
			
			view.registerObserver(ViewTestNote.NAME, observer);
			view.notifyObservers(note);
			
			assertSame('The observer was notified', note.getBody(), this.viewTestVar)
		}
		
	,	testRegisterAndRetrieveMediator: function ()
		{
			var view= View.getInstance()
			,	viewTestMediator= new ViewTestMediator(this)
			,	mediator
			
			view.registerMediator(viewTestMediator);

			assertTrue('The mediator was retrieved successfully', view.retrieveMediator(ViewTestMediator.NAME) === viewTestMediator);
			
			this.cleanup();
		}
		
	,	testHasMediator: function ()
		{
			var view= View.getInstance()
			,	mediatorName= 'hasMediatorTest'
			,	mediator= new Mediator(mediatorName, this)
			
			view.registerMediator(mediator);
			
			assertTrue('The view has the mediator', view.hasMediator(mediatorName));
			
			view.removeMediator(mediatorName);
			
			assertFalse('The view does not have the mediator', view.hasMediator(mediatorName));
		}
		
	,	testRegisterAndRemoveMediator: function ()
		{
			var view= View.getInstance()
			,	mediatorName= 'testing'
			,	mediator= new Mediator(mediatorName, this)
			,	removedMediator= null
			
			view.registerMediator(mediator);
			removedMediator= view.removeMediator(mediatorName);
			assertTrue('The mediator was returned as a result of being removed', mediator === removedMediator);
			assertTrue('The view no longer has the mediator', null == view.retrieveMediator(mediatorName));
			
			this.cleanup();
		}
		
	,	testOnRegisterAndRemove: function ()
		{
			var view= View.getInstance()
			,	mediator= new ViewTestMediator4(this)
			
			view.registerMediator(mediator);
			assertTrue('The mediator was registered by the view', this.onRegisterCalled);
			
			view.removeMediator(mediator.getMediatorName());
			
			assertTrue('The mediator was removed from the view', this.onRemoveCalled);
			
			this.cleanup();
		}
		
	,	testSuccessiveRegisterAndRemoveMediator: function ()
		{
			var view= View.getInstance()
			
			view.registerMediator(new ViewTestMediator(this));
			assertInstanceOf('The View registered the first mediator', ViewTestMediator, view.retrieveMediator(ViewTestMediator.NAME));
			
			view.removeMediator(ViewTestMediator.NAME);
			
			assertTrue('The view did not return the mediator after removal', 
				null == view.retrieveMediator(ViewTestMediator.NAME));
			assertTrue('There were no issues attempting to re-retrieve the mediator after removal', 
				'undefined' === typeof view.retrieveMediator(ViewTestMediator.NAME));
		
			view.registerMediator(new ViewTestMediator(this));
			assertInstanceOf('The View registered the new instance of ViewTestMediator', 
				ViewTestMediator, view.retrieveMediator(ViewTestMediator.NAME));
			
			view.removeMediator(ViewTestMediator.NAME);
			
			assertTrue('The view did not return the mediator after removal after re-registration and removal', 
				null == view.retrieveMediator(ViewTestMediator.NAME));
				
			this.cleanup();
		}
		
	,	testRemoveMediatorAndSubsequentNotify: function ()
		{
			var view= View.getInstance()
			
			view.registerMediator(new ViewTestMediator2(this));
			
			// test that notifications work
			view.notifyObservers(new Notification(ViewTestConstants.NOTE1));
			assertSame('The first notification was sent correctly', ViewTestConstants.NOTE1, this.lastNotification);
			
			view.notifyObservers(new Notification(ViewTestConstants.NOTE2));
			assertSame('The second notification was sent correctly', ViewTestConstants.NOTE2, this.lastNotification);
			
			view.removeMediator(ViewTestMediator2.NAME);
			
			assertTrue('The mediator was removed correctly', null == view.retrieveMediator(ViewTestMediator2.NAME));
			
			this.lastNotification= null;
			
			view.notifyObservers(new Notification(ViewTestConstants.NOTE3));
			
			assertTrue('The notification was sent (NOTE1), but the mediator was not notified', this.lastNotification != ViewTestConstants.NOTE1);
			assertTrue('The notification was sent (NOTE2), but the mediator was not notified', this.lastNotification != ViewTestConstants.NOTE2);
		
			this.cleanup();
		}
		
	,	testRemoveOneOfTwoMediatorsAndSubsequentlyNotify: function ()
		{
  			// Get the Singleton View instance
  			var view = View.getInstance();
			
			// Create and register that responds to notifications 1 and 2
			view.registerMediator( new ViewTestMediator2( this ) );
			
			// Create and register that responds to notification 3
			view.registerMediator( new ViewTestMediator3( this ) );
			
			// test that all notifications work
   			view.notifyObservers( new Notification(ViewTestConstants.NOTE1) );
   			assertTrue( "Expecting lastNotification == NOTE1", 
		   			this.lastNotification == ViewTestConstants.NOTE1);

   			view.notifyObservers( new Notification(ViewTestConstants.NOTE2) );
   			assertTrue( "Expecting lastNotification == NOTE2", 
		   			this.lastNotification == ViewTestConstants.NOTE2);

   			view.notifyObservers( new Notification(ViewTestConstants.NOTE3) );

   			assertTrue( "Expecting lastNotification == NOTE3", 
		   			this.lastNotification == ViewTestConstants.NOTE3);
	
			// Remove the Mediator that responds to 1 and 2
			view.removeMediator( ViewTestMediator2.NAME );

			// test that retrieving it now returns null			
   			assertTrue( "Expecting view.retrieveMediator( ViewTestMediator2.NAME ) == null", 
   			view.retrieveMediator( ViewTestMediator2.NAME ) == null );

			// test that notifications no longer work
			// for notifications 1 and 2, but still work for 3
			this.lastNotification = null;
			
   			view.notifyObservers( new Notification(ViewTestConstants.NOTE1) );
   			assertTrue( "Expecting lastNotification != NOTE1", 
		   			this.lastNotification != ViewTestConstants.NOTE1);

   			view.notifyObservers( new Notification(ViewTestConstants.NOTE2) );
   			assertTrue( "Expecting lastNotification != NOTE2", 
		   			this.lastNotification != ViewTestConstants.NOTE2);
		   			
		   	view.notifyObservers( new Notification(ViewTestConstants.NOTE3) );
   			assertTrue( "Expecting lastNotification == NOTE3", 
		   			this.lastNotification == ViewTestConstants.NOTE3);

			this.cleanup();			
		}
		
	,	viewTestMethod: function (note)
		{
			this.viewTestVar= note.getBody();
		}
		
	,	cleanup: function ()
		{
			View.getInstance().removeMediator(ViewTestMediator.NAME);
			View.getInstance().removeMediator(ViewTestMediator2.NAME);
			View.getInstance().removeMediator(ViewTestMediator3.NAME);
		}
	}
);
